package com.cskaoyan.day24.tcp;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 8888);
        OutputStream out = socket.getOutputStream();
        String str = new Scanner(System.in).nextLine();
        int length = str.getBytes().length;
        DataOutputStream dataOutputStream = new DataOutputStream(out);
        dataOutputStream.writeLong(length);
        out.write(str.getBytes());
        System.out.println("客户端字符串已经写入成功");

        System.out.println("开始写入文件");
        File file = new File("zclient/a.txt");
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        BufferedWriter out1 = new BufferedWriter(new OutputStreamWriter(out));
        String str1;
        while (  (str1 = in.readLine()) != null) {
            out1.write(str1);
            out1.write('\n');
            out1.flush();
        }
        out1.write("end");
        out1.write('\n');
        out1.flush();
        System.out.println("文件写入成功！");


        in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

        while (  (str1 = in.readLine()) != null) {
            out1.write(str1);
            out1.write('\n');
            out1.flush();
        }
        out1.write("end");
        out1.write('\n');
        out1.flush();
        System.out.println("文件写入成功！");
        out1.close();
        in.close();
        socket.close();
    }
}
